﻿namespace SuperMarieApi.App.Controllers.Framework.DataAccess.SqlBuilder
{
    using System;

    public abstract class Condition
    {
        protected string GetOperatorString(SqlOperator op)
        {
            switch (op)
            {
                case SqlOperator.Equal: return "=";
                case SqlOperator.NotEqual: return "<>";
                case SqlOperator.MoreThan: return ">";
                case SqlOperator.MoreThanOrEqual: return ">=";
                case SqlOperator.LessThan: return "<";
                case SqlOperator.LessThanOrEqual: return "<=";

                case SqlOperator.LeftLike:
                case SqlOperator.RightLike:
                case SqlOperator.Like:
                    return "LIKE";
                case SqlOperator.In: return "IN";
                case SqlOperator.NotIn: return "NOT IN";
                case SqlOperator.IsNull: return "IS NULL";
                case SqlOperator.IsNotNull: return "IS NOT NULL";
                default:
                    return string.Empty;
            }
        }

        protected string GetRelationString(SqlRelation relation)
        {
            if (relation == SqlRelation.AND)
            {
                return " AND ";
            }

            return " OR ";
        }

        protected string GetLikeValue(object value)
        {
            if (value != null && value is string)
            {
                return "%" + value.ToString().Trim() + "%";
            }

            throw new ArgumentException("Like value cannot be null or empty.");
        }

        protected string GetLeftLikeValue(object value)
        {
            if (value != null && value is string)
            {
                return value.ToString().Trim() + "%";
            }

            throw new ArgumentException("LeftLike value cannot be null or empty.");
        }

        protected string GetRightLikeValue(object value)
        {
            if (value != null && value is string)
            {
                return "%" + value.ToString().Trim();
            }

            throw new ArgumentException("RightLike value cannot be null or empty.");
        }

        public abstract string Build2String(DataCommand dbCommand);

        public abstract string GetPrepend();
    }
}
